//
//  PopularLoansView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/12/17.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

@objc protocol PopularLoansViewDelegate {
    @objc func changeTableViewFrame(count:Int) //修改坐标
    @objc func selectLoanType(model:GenericLoansModel) //选择贷款类型
}

class PopularLoansView: UIView {
    
    weak var delegate : PopularLoansViewDelegate?
    
    fileprivate var bankName  = "" //放款机构
    
    fileprivate let cellId = "popularLoansCell"
    fileprivate var dataArray = [GenericLoansModel]()
    fileprivate var tableView : UITableView!

    init(bankName:String) {
        super.init(frame: .zero)
        
        self.bankName = bankName
        
        tableView = UITableView.init(frame: .zero, style: .plain)
        tableView.backgroundColor = UIColor.clear
        tableView.separatorStyle = .none
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(NewCityLoanCell.self, forCellReuseIdentifier: cellId)
        self.addSubview(tableView)
        
        let headView = NewLoanHeadView.init(frame: .init(x: 0, y: 0, width: ScreenW, height: RATIO_H(maxNum: 40)))
        headView.text = bankName + popularLoans
        headView.isFlag = true
        headView.moreButton.isHidden = true
        headView.arrowButton.isHidden = true
        tableView.tableHeaderView = headView
        
        tableView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.zero)
        }
        
        getData()
    }
    
    //MARK: 加载数据
    public func getData() {
        
        KMTool.requestMethod(.get, URLString: url_genericLoans, parameters: ["psw":"kameng98_loan","p":"1","organization_list":self.bankName], success: { (value, json) in
            
            if let array = json["data"].arrayObject as? [[String:Any]] {
                
                if array.count == 0 {
                    return
                }else{
                    self.dataArray.removeAll()
                }
                
                for item in array {
                    let model = GenericLoansModel.setModel(with: item)
                    self.dataArray.append(model)
                    
                    if self.dataArray.count == 5 {
                        break
                    }
                }
                
                self.tableView.reloadData()
            }
            
            self.delegate?.changeTableViewFrame(count: self.dataArray.count)
            
        }) {
            
            self.delegate?.changeTableViewFrame(count: self.dataArray.count)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension PopularLoansView : UITableViewDelegate,UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return dataArray.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return RATIO_H(maxNum: 140)
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as? NewCityLoanCell
        cell?.model = dataArray[indexPath.row]
        return cell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        self.delegate?.selectLoanType(model: dataArray[indexPath.row])
    }
}
